package com.levara.bonbon;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;


public class Main extends Activity {
	private NotificationManager mNotificationManager;
	private int SIMPLE_NOTIFICATION_ID; 
		
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        final Context context = getApplicationContext();
                
        mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
		final Notification notifyDetails = new Notification(R.drawable.icon,"BonBon stanje osvježeno!",System.currentTimeMillis());
		
		final Button refresh = (Button)findViewById(R.id.refresh);
		
		refresh.setOnClickListener(new OnClickListener() 
		{
			public void onClick(View v)
			{
				Toast.makeText(getBaseContext(), "Pokrenuto osvježavanje stanja, šaljem sms.", Toast.LENGTH_SHORT).show();
				String SENT = "SMS_SENT";
		    	String DELIVERED = "SMS_DELIVERED";
		    	
		        PendingIntent sentPI = PendingIntent.getBroadcast(context, 0,
		            new Intent(SENT), 0);
		        
		        PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0,
		            new Intent(DELIVERED), 0);
		    	
		        //---when the SMS has been sent---
		        registerReceiver(new BroadcastReceiver(){
					@Override
					public void onReceive(Context arg0, Intent arg1) {
						switch (getResultCode())
						{
						    case Activity.RESULT_OK:
							    Toast.makeText(getBaseContext(), "sms poslan.", 
							    		Toast.LENGTH_SHORT).show();
							    break;
						    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
							    Toast.makeText(getBaseContext(), "Generic failure", 
							    		Toast.LENGTH_SHORT).show();
							    refresh.setClickable(true);
							    break;
						    case SmsManager.RESULT_ERROR_NO_SERVICE:
							    Toast.makeText(getBaseContext(), "No service", 
							    		Toast.LENGTH_SHORT).show();
							    refresh.setClickable(true);
							    break;
						    case SmsManager.RESULT_ERROR_NULL_PDU:
							    Toast.makeText(getBaseContext(), "Null PDU", 
							    		Toast.LENGTH_SHORT).show();
							    refresh.setClickable(true);
							    break;
						    case SmsManager.RESULT_ERROR_RADIO_OFF:
							    Toast.makeText(getBaseContext(), "Radio off", 
							    		Toast.LENGTH_SHORT).show();
							    refresh.setClickable(true);
							    break;
						}
					}
		        }, new IntentFilter(SENT));
		        
		        //---when the SMS has been delivered---
		        registerReceiver(new BroadcastReceiver(){
					@Override
					public void onReceive(Context arg0, Intent arg1) {
						switch (getResultCode())
						{
						    case Activity.RESULT_OK:
							    Toast.makeText(getBaseContext(), "sms dostavljen.", 
							    		Toast.LENGTH_SHORT).show();
							    finish();
							    break;
						    case Activity.RESULT_CANCELED:
							    Toast.makeText(getBaseContext(), "sms nije dostavljen.", 
							    		Toast.LENGTH_SHORT).show();
							    break;					    
						}
					}
		        }, new IntentFilter(DELIVERED));        
		        SmsManager sms = SmsManager.getDefault();
		        sms.sendTextMessage("0977", null, "stanje", sentPI, deliveredPI);  
		        refresh.setClickable(false);
		        
//		        Uri uri = Uri.parse("content://sms/inbox");
//		        
//				Cursor cursor = getContentResolver().query(uri, null, null, null, null);
//		        while (cursor.moveToNext()) 
//		        {
//		        	String poruka = new String(cursor.getBlob(cursor.)
//		        }


			}
		});
    }
}